<?php 
 class Administration extends CI_Controller {
 	
 	public function __construct()
 	{
 		log_message('info', 'Start Login contructor');
 		parent::__construct();
 		$this->load->library('Template');
 		require_once 'Mobile_Detect.php';
 		$detect = new Mobile_Detect;
 		$mobile = array('isMobile' => $detect->isMobile() == 1 ? true : false,
 				'isTablet' => $detect->isTablet() == 1 ? true : false,
 				'isPhone' => ($detect->isMobile() && !$detect->isTablet()) == 1 ? true : false,
 				'isIE7' => ($detect->version('MSIE')==7.0) == 1 ? true : false,
 				'isIE8' => ($detect->version('MSIE')==8.0) == 1 ? true : false,
 				'isIE9' => ($detect->version('MSIE')==9.0) == 1 ? true : false
 		);
 		$this->load->vars($mobile);
 		log_message('info', 'End Login contructor');
 	}
 	
 	function index() {
 		log_message('info', 'Start index');

 		$data = array();
 		
 		$this->template->set_template('layout');
//  		$this->addNavbar($data);
 		$this->template->write_view('contentRegion', 'admin/login_form', $data, TRUE);
//  		$this->addFooter($data);
 		
 		$layout_message = $this->template->render('', TRUE);
 		$this->template->set_template('base');
 		$this->addImports();
 		$this->template->write('layoutRegion', $layout_message);
 		$this->template->render();
 		log_message('info', 'End index');
 	} 
 	
 	function validate_credentials() {
 		$this->load->model('membership_model');
 		
 		$query = $this->membership_model->validate_nodb();
 		if($query) {
 			$data = array(
 					'username' => $this->input->post('username'),
 					'is_logged_in' => true
 			);
 			
 			$this->session->set_userdata($data);
 			redirect('admin/home');
 		} else {
 			$this->index();
 		}
 		
 	}
 	
 	function home()
 	{
 		if($this->is_logged_in())
 		{
 			$data = array();
 		  
 			$this->template->set_template('layout');
 			$this->addNavbar($data);
 			$this->template->write_view('contentRegion', 'admin/home', $data, TRUE);
 			$this->addFooter($data);
 		  
 			$layout_message = $this->template->render('', TRUE);
 		  
 			$this->template->set_template('base');
 			$this->addImports();
 			$this->template->write('layoutRegion', $layout_message);
 			$this->template->render();
 		}
 	}
 	
 	function foto()
 	{
 		if($this->is_logged_in())
 		{
 			$data = array();
 				
 			$this->template->set_template('layout');
 			$this->addNavbar($data);
 			$this->template->write_view('contentRegion', 'admin/foto', $data, TRUE);
 			$this->addFooter($data);
 				
 			$layout_message = $this->template->render('', TRUE);
 				
 			$this->template->set_template('base');
 			$this->addImports();
 			$this->template->write('layoutRegion', $layout_message);
 			$this->template->render();
 		}
 	}
 	
 	function video()
 	{
 		if($this->is_logged_in())
 		{
 			$data = array();
 				
 			$this->template->set_template('layout');
 			$this->addNavbar($data);
 			$this->template->write_view('contentRegion', 'admin/video', $data, TRUE);
 			$this->addFooter($data);
 				
 			$layout_message = $this->template->render('', TRUE);
 				
 			$this->template->set_template('base');
 			$this->addImports();
 			$this->template->write('layoutRegion', $layout_message);
 			$this->template->render();
 		}
 	}
 	
 	function musica()
 	{
 		if($this->is_logged_in())
 		{
 			$data = array();
 				
 			$this->template->set_template('layout');
 			$this->addNavbar($data);
 			$this->template->write_view('contentRegion', 'admin/musica', $data, TRUE);
 			$this->addFooter($data);
 				
 			$layout_message = $this->template->render('', TRUE);
 				
 			$this->template->set_template('base');
 			$this->addImports();
 			$this->template->write('layoutRegion', $layout_message);
 			$this->template->render();
 		}
 	}
 	
 	function news()
 	{
 		if($this->is_logged_in())
 		{
 			$data = array();
 				
 			$this->template->set_template('layout');
 			$this->addNavbar($data);
 			$this->template->write_view('contentRegion', 'admin/news', $data, TRUE);
 			$this->addFooter($data);
 				
 			$layout_message = $this->template->render('', TRUE);
 				
 			$this->template->set_template('base');
 			$this->addImports();
 			$this->template->write('layoutRegion', $layout_message);
 			$this->template->render();
 		}
 	}
 	
 	function is_logged_in() {
 		log_message('info', ' Start is_logged_in');
 		$is_logged_in = $this->session->userdata('is_logged_in');
 		if(!isset($is_logged_in) || $is_logged_in != true) {
 			//no permission to load this page!!

 			$data = array();
 				
 			$this->template->set_template('layout');
//  			$this->addNavbar($data);
 			$this->template->write_view('contentRegion', 'admin/nologin', $data, TRUE);
//  			$this->addFooter($data);
 				
 			$layout_message = $this->template->render('', TRUE);
 				
 			$this->template->set_template('base');
 			$this->addImports();
 			$this->template->write('layoutRegion', $layout_message);
 			$this->template->render();
 			
 			return false;
 		}
 		 
 		return true;
 	}
 	
 	function signup() {
 		$data['main_content'] = 'admin/signup_form';
 		$this->load->view('admin/includes/template', $data);
 	}
 	
 	function create_member() {
 		$this->load->library('form_validation');
 		
 		//field name, error message, validation rules
 		$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
 		$this->form_validation->set_rules('last_name', 'Last name', 'trim|required');
 		$this->form_validation->set_rules('email_address', 'Email address', 'trim|required|valid_email');
 		
 		$this->form_validation->set_rules('username', 'username', 'trim|required|min_length[4]');
 		$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]');
 		$this->form_validation->set_rules('password2', 'password confirmation', 'trim|required|matches[password]');
 		
 		if($this->form_validation->run() == FALSE) {
 			$this->signup();
 		} else {
 			log_message('info', 'val true');
 			$this->load->model('membership_model');
 			if($this->membership_model->create_member()) {
 				$data['main_content'] = 'admin/signup_successful';
 				$this->load->view('admin/includes/template', $data);
 			} else {
 				$this->load->view('signup_form'); 			
 			}
 		}
 	}
 	
 	function logout() {
 		$this->session->sess_destroy();
 		redirect('admin');
 	}

 	
 	function addNavbar($data) {
 		$this->template->write_view('navbarPhoneRegion', 'admin/components-phone/navbar', $data, TRUE);
 		$this->template->write_view('navbarTabletRegion', 'admin/components-tablet/navbar', $data, TRUE);
 		$this->template->write_view('navbarDesktopRegion', 'admin/components-desktop/navbar', $data, TRUE);
 	}
 	
 	function addFooter($data) {
 		$this->template->write_view('footerPhoneRegion', 'admin/components-phone/footer', $data, TRUE);
 		$this->template->write_view('footerTabletRegion', 'admin/components-tablet/footer', $data, TRUE);
 		$this->template->write_view('footerDesktopRegion', 'admin/components-desktop/footer', $data, TRUE);
 	}
 	
 	
 	
 	function addImports() {
 		$version = rand();
 		 
 		$detect = new Mobile_Detect;
 		//including general CSS libraries
 		$this->template->add_css('css/admin/bootstrap.min.css?v='.$version);
 		$this->template->add_css('css/admin/normalize.css?v='.$version);
 		$this->template->add_css('css/admin/averonesi.css?v='.$version);
 		 
 		if ($detect->isMobile()) {
 			//mobile
 			$this->template->add_css('css/admin/averonesi-mobile.css?v='.$version);	//main css for tablet and phones
 		}
 		if($detect->isTablet()) {
 			//tablet
 	
 			$this->template->add_css('css/admin/averonesi-tablet.css?v='.$version);	//mobile override for tablet
 			$this->template->add_css('css/admin/averonesi-desktop.css?v='.$version);	//in landscape a tablet behave like a desktop
 		} else if ($detect->isMobile() && !$detect->isTablet()) {
 			//phone
 			$this->template->add_css('css/admin/averonesi-phone.css?v='.$version);	//mobile override for phone
 		} else {
 			//computer and unknown
 			if($detect->version('MSIE')==7.0) {
 				$this->template->add_css('css/admin/averonesi-ie7.css?v='.$version);
 			} else if($detect->version('MSIE')==8.0) {
 				$this->template->add_css('css/admin/averonesi-ie8.css?v='.$version);
 			} else if($detect->version('MSIE')==9.0) {
 				$this->template->add_css('css/admin/averonesi-ie9.css?v='.$version);
 			} else {
 					
 				$this->template->add_css('css/admin/averonesi-desktop.css?v='.$version);
 			}
 		}
 		 
 		 
 		 
 		//including general JS libraries
 		$this->template->add_js('js/jquery-1.9.1.min.js?v='.$version);
 		$this->template->add_js('js/jquery-ui-1.10.2.effects.js?v='.$version);
 		$this->template->add_js('js/underscore-min.js?v='.$version);
 		$this->template->add_js('js/modernizr.custom.63321.js?v='.$version);
 		$this->template->add_js('js/yepnope.js?v='.$version);
 		$this->template->add_js('js/admin/plugins.js?v='.$version);
 		$this->template->add_js('js/admin/main.js?v='.$version);
 		 
 		if ($detect->isMobile()) {
 			//mobile
 			$this->template->add_js('js/admin/averonesi-mobile.js?v='.$version);	//main js for tablet and phones
 		}
 		if($detect->isTablet()) {
 			//tablet
 	
 			$this->template->add_js('js/admin/averonesi-tablet.js?v='.$version);	//tablet specific js ex. javascript to be executed only on tablets devices
 			$this->template->add_js('js/admin/averonesi-desktop.js?v='.$version);	//in landscape a tablet behave like a desktop
 		} else if ($detect->isMobile() && !$detect->isTablet()) {
 			//phone
 			$this->template->add_js('js/admin/averonesi-phone.js');	//phone specific js ex. javascript to be executed only on phones devices
 		} else {
 			//computer
 			if($detect->version('MSIE')==7.0) {
 				$this->template->add_js('js/admin/averonesi-ie7.js?v='.$version);
 			} else if($detect->version('MSIE')==8.0) {
 				$this->template->add_js('js/admin/averonesi-ie8.js?v='.$version);
 			} else if($detect->version('MSIE')==9.0) {
 				$this->template->add_js('js/admin/averonesi-ie9.js?v='.$version);
 			} else {
 				$this->template->add_js('js/admin/averonesi-desktop.js?v='.$version);
 			}
 		}
 	}
 }

